package cn.wolfcode.luowowo.advice;

import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonExceptionHandler {

    // 给用户看的异常处理抛到页面
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object handlerLogic(Exception e){

        // 捕获异常:方便开发在后台观看异常信息
        e.printStackTrace();
        // 若用户输入框某个参数没输入,则返回提示信息
        return new JsonResult<>(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
    }


    // 运行或系统出现异常抛到页面
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object handlerException(Exception e){
        e.printStackTrace();

        // 返回系统异常信息给用户看
        return JsonResult.defaultError();
    }
}
